package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallGoodsProductMapper;
import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
import org.linlinjava.litemall.db.domain.LitemallGoodsProductExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LitemallGoodsProductService {

    @Resource
    private LitemallGoodsProductMapper productMapper;

    public int count(){
        LitemallGoodsProductExample productExample=new LitemallGoodsProductExample();
        productExample.or().andDeletedEqualTo(false);

        return (int) productMapper.countByExample(productExample);
    }

    public List<LitemallGoodsProduct> queryByGid(Integer gid) {
        LitemallGoodsProductExample example = new LitemallGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return productMapper.selectByExample(example);
    }

    public LitemallGoodsProduct findProductById(Integer id){
        return productMapper.selectByPrimaryKey(id);
    }

    public int addStock(Integer id, Short num){
        return productMapper.addStock(id, num);
    }

    public int reduceStock(Integer productId,short cartNum){
        LitemallGoodsProduct pro = findProductById(productId);
        pro.setNumber(pro.getNumber() - cartNum);
        LitemallGoodsProductExample example = new LitemallGoodsProductExample();
        example.createCriteria().andIdEqualTo(productId).andDeletedEqualTo(false);
        return productMapper.updateByExampleSelective(pro,example);
    }

}
